home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1994 October / Macformat17.cdr / Shareware City / Developers / shutdown-fx-201-c / sfx ƒ / sfx control app ƒ / sfx code ƒ / file management.c < prev    next >
Text File  |  1994-07-11  |  6KB  |  233 lines

  1. /**********************************************************************\
  2.  
  3. File:        file management.c
  4.  
  5. Purpose:    This module handles files for deBinHexing: opening the
  6.             input file, creating/opening the temp file, reading and
  7.             writing files, and finalizing them when we're done.
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program in a file named "GNU General Public License".
  21. If not, write to the Free Software Foundation, 675 Mass Ave,
  22. Cambridge, MA 02139, USA.
  23.  
  24. \**********************************************************************/
  25.  
  26. #include "Script.h"
  27. #include "file management.h"
  28. #include "environment.h"
  29. #include "util.h"
  30.  
  31. unsigned long    theFileType, theFileCreator;
  32. short            theFileFlags;
  33. unsigned long    theCreationDate, theModificationDate;
  34. FSSpec            inputFS, outputFS, tempFS;
  35. Boolean            deleteBinHexFile;
  36. short            inputRefNum, outputDFRefNum, outputRFRefNum;
  37. unsigned long    outputDFeof, outputRFeof;
  38.  
  39. void InitFiles(void)
  40. {
  41.     inputRefNum=outputDFRefNum=outputRFRefNum=0;
  42.     outputDFeof=outputRFeof=0L;
  43.     deleteBinHexFile=TRUE;
  44. }
  45.  
  46. enum ErrorTypes OpenInputFile(void)
  47. {
  48.     OSErr            isHuman;
  49.     HParamBlockRec    paramBlock;
  50.     
  51.     if (gHasFSSpecs)
  52.         isHuman=FSpOpenDF(&inputFS, fsRdPerm, &inputRefNum);
  53.     else
  54.         isHuman=HOpen(inputFS.vRefNum, inputFS.parID, inputFS.name,
  55.                         fsRdPerm, &inputRefNum);
  56.     if (!isHuman)
  57.     {
  58.         SetFPos(inputRefNum, 1, 0L);
  59.         
  60.         paramBlock.fileParam.ioCompletion=0L;
  61.         paramBlock.fileParam.ioNamePtr=inputFS.name;
  62.         paramBlock.fileParam.ioVRefNum=inputFS.vRefNum;
  63.         paramBlock.fileParam.ioFDirIndex=0;
  64.         paramBlock.fileParam.ioDirID=inputFS.parID;
  65.         PBHGetFInfo(¶mBlock, FALSE);
  66.         theCreationDate=paramBlock.fileParam.ioFlCrDat;
  67.         theModificationDate=paramBlock.fileParam.ioFlMdDat;
  68.     }
  69.     else return kCantOpenInputFile;
  70. }
  71.  
  72. enum ErrorTypes CreateTempFile(void)
  73. {
  74.     short                i;
  75.     Str255                timeStr;
  76.     OSErr                isHuman;
  77.     enum ErrorTypes        resultCode;
  78.     
  79.     tempFS=outputFS;
  80.     tempFS.name[0]=0x04;
  81.     tempFS.name[1]='H';
  82.     tempFS.name[2]='Q';
  83.     tempFS.name[3]='X';
  84.     tempFS.name[4]='.';
  85.     NumToString(Time&0x7fffffff, timeStr);
  86.     for (i=1; i<=timeStr[0]; i++)
  87.         tempFS.name[++(tempFS.name[0])]=timeStr[i];
  88.     
  89.     if (gHasFSSpecs)
  90.         isHuman=FSpCreate(&tempFS, CREATOR, 'TeMp', smSystemScript);
  91.     else
  92.         isHuman=HCreate(tempFS.vRefNum, tempFS.parID, tempFS.name, CREATOR, 'TeMp');
  93.     
  94.     return (isHuman==noErr) ? allsWell : kCantCreateTempFile;
  95. }
  96.  
  97. enum ErrorTypes SetupTempFile(void)
  98. {
  99.     OSErr                isHuman;
  100.     
  101.     isHuman=noErr;
  102.     
  103.     if (outputDFeof!=0)
  104.     {
  105.         if (gHasFSSpecs)
  106.             isHuman=FSpOpenDF(&tempFS, fsRdWrPerm, &outputDFRefNum);
  107.         else
  108.             isHuman=HOpen(tempFS.vRefNum, tempFS.parID,
  109.                             tempFS.name, fsRdWrPerm, &outputDFRefNum);
  110.     }
  111.     
  112.     if (isHuman!=noErr)
  113.         return kDiskWriteErr;
  114.     
  115.     if (outputRFeof!=0)
  116.     {
  117.         if (gHasFSSpecs)
  118.             isHuman=FSpOpenRF(&tempFS, fsRdWrPerm, &outputRFRefNum);
  119.         else
  120.             isHuman=HOpenRF(tempFS.vRefNum, tempFS.parID,
  121.                             tempFS.name, fsRdWrPerm, &outputRFRefNum);
  122.     }
  123.     
  124.     if (isHuman!=noErr)
  125.         return kDiskWriteErr;
  126.     
  127.     if (outputDFeof!=0)
  128.         isHuman=SetEOF(outputDFRefNum, outputDFeof);
  129.     if ((!isHuman) && (outputRFeof!=0))
  130.         isHuman=SetEOF(outputRFRefNum, outputRFeof);
  131.  
  132.     if (!isHuman)
  133.     {
  134.         FlushVol(0L, tempFS.vRefNum);
  135.         if (outputDFeof!=0)
  136.             SetFPos(outputDFRefNum, 1, 0L);
  137.         if (outputRFeof!=0)
  138.             SetFPos(outputRFRefNum, 1, 0L);
  139.     }
  140.     
  141.     return (isHuman ? kDiskWriteErr : allsWell);
  142. }
  143.  
  144. void FinalizeFiles(Boolean good)
  145. {
  146.     HParamBlockRec        paramBlock;
  147.     FInfo                theInfo;
  148.     
  149.     if (inputRefNum)
  150.         FSClose(inputRefNum);
  151.     
  152.     if (outputDFRefNum)
  153.         FSClose(outputDFRefNum);
  154.     if (outputRFRefNum)
  155.         FSClose(outputRFRefNum);
  156.     
  157.     FlushVol(0L, tempFS.vRefNum);
  158.     
  159.     if (!good)
  160.     {
  161.         if(gHasFSSpecs)
  162.             FSpDelete(&tempFS);
  163.         else
  164.             HDelete(tempFS.vRefNum, tempFS.parID, tempFS.name);
  165.         
  166.         return;
  167.     }
  168.     
  169.     if (deleteBinHexFile)
  170.     {
  171.         if (gHasFSSpecs)
  172.             FSpDelete(&outputFS);
  173.         else
  174.             HDelete(outputFS.vRefNum, outputFS.parID, outputFS.name);
  175.     }
  176.     FlushVol(0L, tempFS.vRefNum);
  177.     
  178.     
  179.     if (gHasFSSpecs)
  180.         FSpGetFInfo(&tempFS, &theInfo);
  181.     else
  182.         HGetFInfo(tempFS.vRefNum, tempFS.parID, tempFS.name,
  183.                     &theInfo);
  184.  
  185.     Mymemset((Ptr)(&theInfo), 0, sizeof(theInfo));
  186.  
  187.     theInfo.fdType=theFileType;
  188.     theInfo.fdCreator=theFileCreator;
  189.     theInfo.fdFlags=theFileFlags;
  190.     theInfo.fdFlags&=~0x0100;    /* clear Inited bit */
  191.     
  192.     if (gHasFSSpecs)
  193.         FSpSetFInfo(&tempFS, &theInfo);
  194.     else
  195.         HSetFInfo(tempFS.vRefNum, tempFS.parID, tempFS.name,
  196.                     &theInfo);
  197.     FlushVol(0L, tempFS.vRefNum);
  198.     
  199.     paramBlock.fileParam.ioCompletion=0L;
  200.     paramBlock.fileParam.ioNamePtr=tempFS.name;
  201.     paramBlock.fileParam.ioVRefNum=tempFS.vRefNum;
  202.     paramBlock.fileParam.ioFDirIndex=0;
  203.     paramBlock.fileParam.ioDirID=tempFS.parID;
  204.     PBHGetFInfo(¶mBlock, FALSE);
  205.     paramBlock.fileParam.ioFlCrDat=theCreationDate;
  206.     paramBlock.fileParam.ioFlMdDat=theModificationDate;
  207.     PBHSetFInfo(¶mBlock, FALSE);    
  208.     FlushVol(0L, tempFS.vRefNum);    
  209.     
  210.     if (gHasFSSpecs)
  211.         FSpRename(&tempFS, outputFS.name);
  212.     else
  213.         HRename(tempFS.vRefNum, tempFS.parID,
  214.                 tempFS.name, outputFS.name);
  215.     
  216.     FlushVol(0L, tempFS.vRefNum);
  217. }
  218.  
  219. enum ErrorTypes ReadInputFile(short thisFile, Ptr buffer, unsigned long count,
  220.     unsigned long *actualCount)
  221. {
  222.     OSErr            isHuman;
  223.     
  224.     *actualCount=count;
  225.     isHuman=FSRead(thisFile, (long*)actualCount, buffer);
  226.     return ((isHuman==eofErr) || (isHuman==noErr)) ? allsWell : kDiskReadErr;
  227. }
  228.  
  229. enum ErrorTypes WriteTempFile(short thatFile, Ptr buffer, unsigned long theLength)
  230. {
  231.     return (FSWrite(thatFile, (long*)(&theLength), buffer)==noErr ? allsWell : kDiskWriteErr);
  232. }
  233.